package pro.jiug.kExcel.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pro.jiug.kExcel.entity.User;
import pro.jiug.kExcel.service.HymaxDztsService;
import pro.jiug.kExcel.service.UserService;
import pro.jiug.kExcel.utils.AnalysisExcelData;
import pro.jiug.kExcel.utils.ExcelUtil;
import pro.jiug.kExcel.utils.InsertISO;
import pro.jiug.kExcel.utils.UploadFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel操作
 * hhh
 * @author Keffett
 * @since 2021-02-19
 */
@Controller
public class ExcelController {

    @Resource
    private InsertISO insertISO;

    @Resource
    private HymaxDztsService hymaxDztsService;

    /*
        北化院
        电子图书数据导入
     */
    @RequestMapping("/dztsImportData")
    @ResponseBody
    public String dztsImportData(HttpServletRequest request,MultipartFile fileUpload, String sqlTableName, String tableName,
                                 int startRow, int endRow, int startCol, int endCol, String rowSign, String tnSign){
        String res="success";
        try {
            hymaxDztsService.dztsImportData(fileUpload, startRow, endRow,startCol,endCol);
        } catch (Exception e) {
            res="error";
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/ImportData")
    @ResponseBody
    public String ImportData(HttpServletRequest request,MultipartFile fileUpload, String sqlTableName, String tableName,
                             int startRow, int endRow, int startCol, int endCol, String rowSign, String tnSign) throws IOException {
        String path = "upload";
//        String fileLocation = request.getServletContext().getRealPath(path);//在idea本身tomcat下建立文件夹
//        String uploadFilePath = UploadFile.upload(fileUpload, fileLocation);
//        List<List<String>> excelList = ExcelUtil.readExcel(uploadFilePath, tableName, startRow, endRow,startCol,endCol);
        List<List<String>> excelList = AnalysisExcelData.getExcelData(fileUpload, startRow, endRow,startCol,endCol);
//        UploadFile.delDir(uploadFilePath);
        List<List<String>> sqlList = new ArrayList<>();
        for (List<String> list : excelList) {
            if(StringUtils.isNotBlank(tnSign) && list.contains(tnSign)){
                break;
            }
            if(StringUtils.isNotBlank(rowSign) && list.contains(rowSign)){
                continue;
            }
            sqlList.add(list);
        }
//        insertCombin.insertList(sqlList,sqlTableName);
        insertISO.insertList(sqlList,sqlTableName); //导入北化院iso电子书专用
        return "Success";
    }

    @RequestMapping("/importData1")
    @ResponseBody
    public String importData1(HttpServletRequest request, MultipartFile fileUpload, String sqlTableName, String tableName,
                             int startRow, int endRow, int startCol, int endCol, String rowSign, String tnSign) throws IOException {
        String path = "upload";
        String fileLocation = request.getServletContext().getRealPath(path);//在idea本身tomcat下建立文件夹
        String uploadFilePath = UploadFile.upload(fileUpload, fileLocation);
        //List<List<String>> excelList = ExcelUtil.readExcel(uploadFilePath, tableName, startRow, endRow,startCol,endCol);
        List<List<String>> excelList = ExcelUtil.readExcel(fileUpload, tableName, startRow, endRow,startCol,endCol);
        UploadFile.delDir(uploadFilePath);
        List<List<String>> sqlList = new ArrayList<>();
        for (List<String> list : excelList) {
            if(StringUtils.isNotBlank(tnSign) && list.contains(tnSign)){
                break;
            }
            if(StringUtils.isNotBlank(rowSign) && list.contains(rowSign)){
                continue;
            }
            sqlList.add(list);
        }
//        insertCombin.insertList(sqlList,sqlTableName);
        insertISO.insertList(sqlList,sqlTableName); //导入北化院iso电子书专用
        return "Success";
    }

    @RequestMapping("/importData2")
    @ResponseBody
    public String importData2(HttpServletRequest request, MultipartFile fileUpload, String sqlTableName, String tableName,
                             int startRow, int endRow, int startCol, int endCol, String rowSign, String tnSign) throws IOException {

        List<List> excelData = AnalysisExcelData.getExcelData2(fileUpload);
        System.out.println(excelData);
        return "Success";
    }

}

